package com.faithbj.printer;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.http.client.ClientProtocolException;

import com.faithbj.printer.service.RemoteInteractive;
import com.faithbj.printer.service.Impl.RemoteInteractiveImpl;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class Html2Word {
	public static void htmlToWord(String html, String docFile) {  
		ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word      
		try {  
			app.setProperty("Visible", new Variant(false));  
			Dispatch docs = app.getProperty("Documents").toDispatch();  
			Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { html, new Variant(false), new Variant(true) }, new int[1]).toDispatch();  
			Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { docFile, new Variant(false) }, new int[1]);  
			Variant f = new Variant(false);  
			Dispatch.call(doc, "Close", f);  
		} catch (Exception e) {  
			e.printStackTrace();  
		} finally {  
			app.invoke("Quit", new Variant[] {});  
			ComThread.Release();  
		}  
	} 
	public static void main(String[] args){
		RemoteInteractive ri = new RemoteInteractiveImpl();
		 String fileName = "D:/work/tmp/ab.doc";
	     String htmlpath = "D:/work/tmp/ab.html";
		try {
			String content = ri.getResource("http://localhost/printer1.jsp");
//			content = new String(content.getBytes(),"utf-8");
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlpath),"gbk"));
			bw.write(content);
			bw.close();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	     htmlToWord(htmlpath,fileName);
	}

}
